(function () {
    var type = utils.getParams(1),
        id = utils.getParams(0),
        url = '/fuli/receive_' + type + '/' + id,
        shareLayer = $( '.share-layer' ).SUIModel({
            shade: '.share-shade'
        }),
        fuliLayer = $( '.fuli-layer' ).SUIModel({
            shade: '.fuli-shade'
        });

    function showText( status, text ) {
        if(status){
            $('.fuli-layer-title').text('恭喜您！领取成功');
            $('.fuli-layer-code').text('激活码：' + text );
            $('.copy-btn').attr('data-clipboard-text', text);
        }else{
            $('.fuli-layer-title').text('抱歉，领取失败');
            $('.fuli-layer-code').text( text );
            $('.fuli-layer-btn').hide();
            $('.fuli-layer-tips').hide();
        }
        fuliLayer.open();
    }
    function getLiBao( $btn ) {
           $.ajax({
               type: "POST",
               url: url,
               dataType:"json",
               success: function (res) {
                   if ( res.ret === 0 ) {
                       //如果返回成功，则openLayer 显示数据
                       //设置btn为已领取
                       setBtnStyle( $btn );
                       if( res.data.codes instanceof Array ){
                           showText( true, res.data.codes[0] );
                       }else if(type === 'coupon'){
                           showText( true, res.data.codes );
                       }
                   } else {
                       showText( false, res.msg );

                   }
               }
           });
    }

    function setBtnStyle( $btn ) {
        $btn.attr('disabled', 'disabled');
        $btn.addClass('libao-btn-disabled');
        $btn.text('已领取');
    }

    function BindGetLibaoEvent( selector ) {
        $( selector ).on('click', function ( e ) {
            e = e || event;
            e.stopPropagation();
            //判断是否登录
            //已登录则发送数据到后台 反馈数据
            if( globalData.login ){
                getLiBao( $(this) );
            }else{
                //更改
                changeTabs( 'tab0' );
                loginModel.open();
            }
        });
    }

    function main() {
        //点击获取礼包
        BindGetLibaoEvent('#getLibao');
        BindGetLibaoEvent('#getTaohao');
        //分享功能

        var clipboard = new Clipboard('.copy-btn');

        $( '.detail-share-btn' ).on('click', function () {
            shareLayer.open();
        });

        $( '.share-layer .common-layer-close' ).on('click', function () {
            shareLayer.close();
        });

        $( '.fuli-layer .common-layer-close' ).on('click', function () {
            fuliLayer.close();
        });

        share();
        upholdText($('.text-upfold'));

    }

    function share() {
        window._bd_share_config = {
            common: {
                bdUrl: window.location.href,
                bdSign: false
            },
            share : {
                "tag" : "share_1",
                "bdSize" : 32,
            }
        }
    }

    function upholdText(selector) {
        selector.on("click",function () {
            selector.toggleClass('text-fold');
            var target = $('.game-text');
            target.toggleClass('height-auto');
        });
    }


    main();

})();